package com.jonniespratley.flexdatabasemanager.utils
{
	public class ByteUtils
	{
		static public  const KBYTES : int = 1;
        static public  const MBYTES : int = 2;
        static public  const GBYTES : int = 3;
        static public  const TBYTES : int = 4;
        static public  const PBYTES : int = 5;
        static public  const EBYTES : int = 6;
        static public  const ZBYTES : int = 7;
        static public  const YBYTES : int = 8;
        static public  const NBYTES : int = 9;
        static public  const DBYTES : int = 10;
        
        private var _formats : Array = [ "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb", "Nb", "Db"];
		
		public function ByteUtils(){}


        public function format( myValue:Number, format:int, digits:int = 0 ):String 
        {
            var divider : Number = Math.pow( 1024, format );
            
            return round( myValue / divider, digits ) + " " + _formats[ format - 1 ];
        }
        
         public function formatToNumber( myValue:Number, format:int, digits:int = 0 ):Number 
        {
            var divider : Number = Math.pow( 1024, format );
            
            return round( myValue / divider, digits );
        }
             
        public function formatToLargest( myValue:Number, digits:int = 0 ):String 
        {
            for (var i : Number = 10; i > 0; i-- ) 
            {
                if ( myValue >= Math.pow( 1024, i ) ) 
                {
                    break;
                }
            }
            return format( myValue, i, digits );
        }
        
		public function round( value:Number, decimals:int = 0 ):Number
		{
        	var rounder : Number = Math.pow( 10, decimals );
        	
        	return Math.round( value * rounder ) / rounder;
    	}

	}
}